package com.central.game.controller;

import com.central.game.model.ResourceInfo;
import com.central.game.service.ResourceService;
import com.central.model.common.PageResult;
import com.central.model.common.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@Slf4j
@RestController
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    //    resource
    @PreAuthorize("hasAuthority('game:get/resource')")
    @ApiOperation(value = "用户查询列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"), @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")})
    @GetMapping("/resource")
    public PageResult<ResourceInfo> getResource(@RequestParam Map<String, Object> params) {
        Date date = new Date();
        PageResult<ResourceInfo> result = resourceService.findResource(params);
        log.info("资源查询耗时:" + (new Date().getTime() - date.getTime()));
        return result;
    }

    @PreAuthorize("hasAuthority('game:get/resource/saveOrUpdate')")
    @ApiOperation(value = "资源修改或添加")
    @PostMapping("/resource/saveOrUpdate")
    public Result resourceSaveOrUpdate(@RequestBody ResourceInfo resourceInfo) {
        try {
            String success;
            if (resourceInfo.getId() != null) {
                resourceService.resourceUpdate(resourceInfo);
                success = "修改成功";
            } else {
                success = "添加成功";
                resourceService.resourceSave(resourceInfo);
            }
            return Result.succeed(success);
        } catch (Exception ex) {
            return Result.failed("操作失败");
        }
    }

    @PreAuthorize("hasAuthority('game:delect/version/delVersion')")
    @ApiOperation(value = "资源删除")
    @DeleteMapping("/resource/del/{id}")
    public Result resourceDel(@PathVariable Long id) {
        try {
            String success;
            resourceService.resourceDel(id);
            success = "删除成功";
            return Result.succeed(success);
        } catch (Exception ex) {
            return Result.failed("删除失败");
        }
    }
}
